package com.study.leetcode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//https://leetcode.cn/problems/majority-element-ii/
public class Solution_229 {
    public List<Integer> majorityElement(int[] nums) {
        Map<Integer, Integer> numCounter = new HashMap<>();
        for (int num : nums) {
            numCounter.merge(num, 1, Integer::sum);
        }
        List<Integer> integers = new ArrayList<>();
        int limit = nums.length / 3;
        for (int num : nums) {
            Integer count = numCounter.get(num);
            if (count > limit && !integers.contains(num)) {
                integers.add(num);
            }
        }
        return integers;
    }
}
